var fs = require('fs'),
	path = require('path'),
	async = require('async');
var log = require('../model/log');

/*
	移动文件
*/
var move = function(connection, getData, getFileID, employeeId, callback) {
	var getDir = "./files/" + getFileID; //找到文件
	if (getData == undefined) {
		callback("no file");
	} else {
		async.forEach(getData, function(item, callback) {
			var tempPath = "./uploads/" + item.tempid;
			var getName = "./files/" + getFileID + "/" + item.name;
			fs.exists(getDir, function(check) {
				if (check == true) {
					fs.readFile(tempPath, function(err, data) {
						var content = {
								fileName: item.name,
								employeeId: getFileID
							};
							var content = JSON.stringify(content);
						fs.writeFile(getName, data, function(err) {
							var logData = {
									itemId: getFileID,
									updatePersonId: employeeId,
									content: content,
									type: '2',
									operation: '上传'
								};
								log.addLog(connection, logData, function(log) {
									console.log('log', log);
									if (log != null && log != undefined)
										console.log('add log success');
								});
						});
					});
				} else {
					fs.mkdir(getDir, 0777, function() {
						fs.readFile(tempPath, function(err, data) {
							var content = {
								fileName: item.name,
								employeeId: getFileID
							};
							var content = JSON.stringify(content);
							fs.writeFile(getName, data, function(err) {
								var logData = {
									itemId: getFileID,
									updatePersonId: employeeId,
									content: content,
									type: '2',
									operation: '上传'
								};
								log.addLog(connection, logData, function(log) {
									console.log('log', log);
									if (log != null && log != undefined)
										console.log('add log success');
								});
							});
						});
					});
				}
			});
		});
		callback("good");
	}
};

module.exports = {
	move: move
};